package com.wcx;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;

/**
 * 项目: zookeeper
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2020-12-29 14:09
 **/
public class ZookeeperClient {
    /**
     * 连接地址
     */
    private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";

    /**
     * session过期时间
     */
    private static int sessionTimeout = 2000;

    /**
     * zk客户端
     */
    private ZooKeeper zkClient = null;


    @Before
    public void init() throws Exception {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {

                // 收到事件通知后的回调函数（用户的业务逻辑）
                System.out.println(event.getType() + "--" + event.getPath());

                // 再次启动监听
                try {
                    zkClient.getChildren("/", true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
